﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace _0712502_0712504_DesignPatternGame
{
    public abstract class GameEntity : VisibleGameEntity
    {
        const int MOVE_UP = -1;
        const int MOVE_DOWN = 1;
        const int MOVE_LEFT = -1;
        const int MOVE_RIGHT = 1;

        protected MoveDirectionState _movingDirection;
        public MoveDirectionState MovingDirection
        {
            get { return _movingDirection; }
            set { _movingDirection = value; }
        }

        protected float _iSpeed;
        public float ISpeed
        {
            get { return _iSpeed; }
            set { _iSpeed = value; }
        }

        Vector2 _vectorSpeedMoving;           // Vector chỉ hướng di chuyển
        public Vector2 VectorSpeedMoving
        {
            get { return _vectorSpeedMoving; }
            set { _vectorSpeedMoving = value; }
        }

        Vector2 _directionMoving;      // Huong di chuyen - Up Down Left Right
        public Vector2 DirectionMoving
        {
            get { return _directionMoving; }
            set { _directionMoving = value; }
        }

        public virtual Vector2 CheckMoveToNextPos(Vector2 curPosition)
        {
            Vector2 newPosition = curPosition;
            _directionMoving = Vector2.Zero;
            _vectorSpeedMoving = Vector2.Zero;
            switch (_movingDirection)
            {

                case MoveDirectionState.left:
                    {
                        this.RotationAngle = MathHelper.ToRadians(270.0f);
                        _vectorSpeedMoving.X = _iSpeed;
                        _directionMoving.X = MOVE_LEFT;
                        _directionMoving = new Vector2((float)Math.Sin(RotationAngle), (float)Math.Cos(RotationAngle));
                        newPosition += _vectorSpeedMoving * _directionMoving;
                        break;
                    }

                case MoveDirectionState.right:
                    {
                        RotationAngle = MathHelper.ToRadians(90.0f);
                        _vectorSpeedMoving.X = _iSpeed;
                        _directionMoving.X = MOVE_RIGHT;
                        _directionMoving = new Vector2((float)Math.Sin(RotationAngle), (float)Math.Cos(RotationAngle));
                        newPosition += _vectorSpeedMoving * _directionMoving;
                        break;
                    }

                case MoveDirectionState.up:
                    {
                        RotationAngle = MathHelper.ToRadians(0.0f);
                        _vectorSpeedMoving.Y = _iSpeed;
                        _directionMoving.Y = MOVE_UP;
                        _directionMoving = new Vector2((float)Math.Sin(RotationAngle), -(float)Math.Cos(RotationAngle));
                        newPosition += _vectorSpeedMoving * _directionMoving;
                        break;
                    }

                case MoveDirectionState.down:
                    {
                        RotationAngle = MathHelper.ToRadians(180.0f);
                        _vectorSpeedMoving.Y = _iSpeed;
                        _directionMoving.Y = MOVE_DOWN;
                        _directionMoving = new Vector2((float)Math.Sin(RotationAngle), -(float)Math.Cos(RotationAngle));
                        newPosition += _vectorSpeedMoving * _directionMoving;
                        break;
                    }
            }

            return newPosition;
        }

        public virtual void Move()
        {
            this.Position = CheckMoveToNextPos(_position);           
        }

        public bool CheckCollisionMapBorder(Rectangle rect) // Kiem tra dung do bien
        {
            if (rect.X < 160 || (rect.X + rect.Width) > 880 ||
                rect.Y < 60 || (rect.Y + rect.Height) > 715)
            {
                return true;
            }
            else
                return false;          
        }     
    }
}
